package com.anuprayog.happypur;

import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

/**
 * @author amit The main game screen. Contains the link to the game, high score
 *         & help screens
 */
public class MainActivity extends Activity {

	public static boolean SOUND_ON;
	public static String SOUND_ON_STRING = "";
	private ToggleButton soundButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		setVolumeControlStream(AudioManager.STREAM_MUSIC);

		soundButton = (ToggleButton) findViewById(R.id.toggleButton1);

		try {
			Intent i = getIntent();

			String soundof = i.getStringExtra(GameOverActivity.SOUND_STATE);
			System.out.println(soundof);

			if (soundof.equals("true")) {
				soundButton.setChecked(true);
			} else if (soundof.equals("false")) {
				soundButton.setChecked(false);
			}

		} catch (NullPointerException e) {

		}

		soundButton
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							SOUND_ON = true;
						} else {
							SOUND_ON = false;
						}

					}
				});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	// link for help screen
	public void openHelp(View view) {
		Intent intent = new Intent(this, HelpActivity.class);
		startActivity(intent);
	}

	// link for game screen
	public void openGame(View view) {
		Intent intent = new Intent(this, GameActivity.class);
		intent.putExtra(SOUND_ON_STRING, String.valueOf(SOUND_ON));
		startActivity(intent);
		finish();
	}

	// link for high score screen
	public void openHigh(View view) {
		Intent intent = new Intent(this, HighActivity.class);
		startActivity(intent);
	}

	@Override
	public void onBackPressed() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setCancelable(false);
		builder.setMessage("Do you want to leave Happypur?");
		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// if user pressed "yes", then he is allowed to exit from
				// application
				finish();
			}
		});
		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// if user select "No", just cancel this dialog and continue
				// with app
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		alert.show();

	}

}
